package yt.kpool.activity.setting.agent.backwallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.adapter.BackWalletAdapter;
import yt.kpool.adapter.MinerListAdapter;
import yt.kpool.bean.BackWalletBean;
import yt.kpool.bean.PoolCoinBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;


/**
 * 返点钱包
 */
public class BackWalletAc extends AppCompatActivity {

    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private BackWalletAdapter backWalletAdapter;
    private List<BackWalletBean.DataBean> list;
    private LoadingDialog dialog;
    private BackWalletBean backWalletBean;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_backwallet);
        ButterKnife.bind(this);
        initView();
        getNetData();
    }

    private void initView()
    {
        dialog = new LoadingDialog(this, 0);
        list=new ArrayList<>();
        EventBus.getDefault().register(this);
        backWalletAdapter=new BackWalletAdapter(BackWalletAc.this,list);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(backWalletAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadMoreEnabled(false);
        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.d_13)
                .setColorResource(R.color.transparent)
                .build();
        mRecyclerView.addItemDecoration(divider);
    }



    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
    public void handleData(String m) {
      Log.d("xxxx",m+"");
        if(m.equals("5510"))
       {
           getNetData();
       }

    }




    @OnClick({R.id.title_left, R.id.ly_add,R.id.title_center})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left://返回
                finish();
                break;
            case R.id.title_center://返回
                finish();
                break;
            case R.id.ly_add://设置钱包
                SetWalletAc.start(BackWalletAc.this,backWalletBean);
                break;

        }
    }

    /**
     * 获取网络数据
     */
    private void getNetData()
    {
        dialog.show();
        NetHelperNew.ReturnWalletList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                dialog.dismiss();
                backWalletBean = MyGson.getInstance().fromJson(data, BackWalletBean.class);
                if(backWalletBean.getType()==1)
                {
                    if(backWalletBean.getData().size()>0)
                    {
                        backWalletAdapter.setListData(backWalletBean.getData());
                        backWalletAdapter.notifyDataSetChanged();
                    }
                    else
                    {
                        ToastUtil.showToast(BackWalletAc.this,"没有可查询数据");
                    }

                }
                else
                {
                    ToastUtil.showToast(BackWalletAc.this,backWalletBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(BackWalletAc.this,msg);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
